const app = getApp();

Page({
  data: {
    hidden: false, //加载
    outline: false, //有无网络
    list: [],
    show: false,
    newsType: 1,
    currentPage: 1,
    pagecount:'',//总数据
    countDownMinute: '',
    countDownSecond: '00',
    noData: false,
    nickName: '',
    imgUrl: '',
    openid: '',
  },
  onLoad(query) {

  },
  getTime() {
    var that = this;
    that.setData({
      hidden: true,
    })
    var req = {
      nickname: that.data.nickName,
      headimgurl: that.data.imgUrl,
      openid: that.data.openid,
      city: '',
      province: '',
      sex: ''
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/noworder/', // 该url是自己的服务地址
      method: 'POST',
      headers: {
      },
      data: req,
      dataType: 'json',
      success: (res) => {// 成功获取订单号
        that.setData({
          hidden: false,
        })
        console.log(res.data.data)
        if (res.data.status == 200) {
          var totalSecond = res.data.data;
          that.setData({
            show: true,
            countDownMinute: e.currentTarget.dataset.time
          })
          // var interval = setInterval(function() {
          //   // 秒数
          //   var second = totalSecond;
          //   // 分钟位
          //   var min = Math.floor((second) / 60);
          //   var minStr = min.toString();
          //   if (minStr.length == 1) minStr = '0' + minStr;
          //   // 秒位
          //   var sec = second - min * 60;
          //   var secStr = sec.toString();
          //   if (secStr.length == 1) secStr = '0' + secStr;
          //   that.setData({
          //     countDownMinute: minStr,
          //     countDownSecond: secStr,
          //   })
          //   totalSecond--;
          //   if (totalSecond < 0) {
          //     clearInterval(interval);
          //     my.showToast({
          //       content: '体验已结束',
          //     });
          //     that.setData({
          //       countDownMinute: '00',
          //       countDownSecond: '00',
          //     })
          //   }
          // }, 1000);
        } else {

          my.showToast({
            content: res.data.message,
          })
        }


      },
      fail: (res) => {
        console.log('GetEmpPrice:', res)
      },
    });
  },
  getData: function() {
    var that = this;
    that.setData({
      hidden: true
    })
    var req = {
      openid: that.data.openid,
      page: that.data.currentPage
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/myorder', // 该url是自己的服务地址
      method: 'POST',
      headers: {
        // 'x-access-token': that.data.token
      },
      data: req,
      dataType: 'json',
      success: (res) => {// 成功获取订单号
        console.log(res)
        that.setData({
          hidden: false
        })
        if (res.data.status == 200) {
          var arr = that.data.list;
          for (var i = 0; i < res.data.data.list.length; i++) {
            arr.push(res.data.data.list[i])
          }
          that.setData({
            list: arr,
            noData: false,
            pagecount:res.data.data.count
          })
          if (that.data.list.length == 0) {
            that.setData({
              noData: true
            })
          }

        } else {
          my.showToast({
            content: res.data.err_msg,
          })
        }


      },
      fail: (res) => {
        console.log('GetEmpPrice:', res)
      },
    });
  },

  refund: function(e) {
    var that = this;
    that.setData({
      hidden: true
    })
    var req = {
      orderid: e.currentTarget.dataset.id
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/Refund', // 该url是自己的服务地址
      method: 'POST',
      headers: {
        // 'x-access-token': that.data.token
      },
      data: req,
      dataType: 'json',
      success: (res) => {// 成功获取订单号
        console.log(res)
        that.setData({
          hidden: false
        })
        if (res.data.status == 200) {
          my.showToast({
            content: '退款成功',
          })

        } else {
          my.showToast({
            content: res.data.message,
          })
        }


      },
      fail: (res) => {
        console.log('GetEmpPrice:', res)
      },
    });
  },
  onReady() {
    // 页面加载完成
  },

  onShow() {
    var that = this;
    my.getNetworkType({
      success: (res) => {
        if (res.networkAvailable) {
          // 获取token
          my.getStorage({
            key: 'token',
            success: function(res) {
              that.setData({
                token: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          // //获取个人信息
          // my.getStorage({
          //   key: 'imgUrl',
          //   success: function(res) {
          //     if (res.data == null) {
          //       my.navigateTo({
          //         url: '/pages/login/login'
          //       });
          //     } else {
          //       that.setData({
          //         imgUrl: res.data
          //       })
          //     }

          //   },

          //   fail: function(res) {
          //     my.alert({ content: res.errorMessage });
          //   }
          // })
          // my.getStorage({
          //   key: 'nickName',
          //   success: function(res) {
          //     if (res.data == null) {
          //       my.navigateTo({
          //         url: '/pages/login/login'
          //       });
          //     } else {
          //       that.setData({
          //         nickName: res.data
          //       })
          //     }

          //   },

          //   //
          //   fail: function(res) {
          //     my.alert({ content: res.errorMessage });
          //   }
          // })
          //获取openid
          my.getStorage({
            key: 'openid',
            success: function(res) {
              that.setData({
                openid: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          that.setData({
            outline: false,
            list:[],
            currentPage:1
          })
          that.getData()
        } else {
          that.setData({
            outline: true
          })
        }
      }
    })
  },
  close: function(e) {
    var that = this;
    that.setData({
      show: false
    })
  },
  active: function(e) {
    var that = this;
    if (e.currentTarget.dataset.id != that.data.newsType) {
      that.setData({
        newsType: e.currentTarget.dataset.id,
        list: []
      })
      if (that.data.newsType == '1') {
        that.setData({
          status: 'pay',
        })
      } else if (that.data.newsType == '2') {
        that.setData({
          status: 'unpay',
        })
      } else if (that.data.newsType == '3') {
        that.setData({
          status: '',
        })
      }
      that.login()
    }
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom: function() {
    var that = this;
    that.setData({
      hidden: false,
    })
    var num = that.data.currentPage;
    num++;
    that.setData({
      currentPage: num
    })
    if (that.data.pagecount < num) {
      my.showToast({
        content: '没有更多数据了',
        duration: 1000
      })
      return;
    }
    that.getData()
  },
  bindKeyInput(e) {
    this.setData({
      code: e.detail.value
    });
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
